package com.khiker.sort;

import java.util.Arrays;
import java.util.Random;

public interface Sort {

	int[] sort(int[] array);

	default void swap(int[] array, int i, int j) {
		if (i == j) {
			return;
		}
		int temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}

	default int[] generateRandomArray(int count) {
		int[] array = new int[count];
		Random random = new Random();
		for (int i = 0; i < count; i++) {
			array[i] = random.nextInt();
		}
		return array;
	}

	default boolean checkMulti(int count) {
		for (int i = 0; i < count; i++) {
//			int[] array = generateRandomArray(3);
			int[] array = generateRandomArray((int) (Math.random() * 10000));
			if (!check(array)) {
				System.out.println("第" + i + "次排序与对数器结果不一致");
				return false;
			}
		}
		System.out.println("经过" + count + "次排序与对数器结果一致");
		return true;
	}

	default boolean check(int[] array) {
		int[] arrayCopy = new int[array.length];
		System.arraycopy(array, 0, arrayCopy, 0, array.length);
//		System.out.println("Source:"+Arrays.toString(array));
		Arrays.sort(arrayCopy);
		sort(array);
		for (int i = 0; i < array.length; i++) {
			if (array[i] != arrayCopy[i]) {
				System.out.println("Jdk第" + i + "位为：" + arrayCopy[i]);
				System.out.println("Self第" + i + "位为：" + array[i]);
				print(arrayCopy, "Jdk:");
				print(array, "Self:");
				return false;
			}
		}
		return true;
	}

	default void print(int[] array, String pre) {
		System.out.println(pre);
		System.out.println(Arrays.toString(array));
	}
}
